#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct stu
{
	char name[20];
	int age;
}stu;

int num_sort(const void* a, const void* b)
{
	return *(int*)a - *(int*)b;
}

int age_sort(const void* a, const void* b)
{
	return ((stu*)a)->age - ((stu*)b)->age;
}

int name_sort(const void* a, const void* b)
{
	return strcmp(((stu*)a)->name, ((stu*)b)->name);
}

void print(int* arr, int num)
{
	for (int i = 0; i < num; i++)
	{
		printf("%d ",arr[i]);
	}
	printf("\n");
}

void swap( char* buf1, char* buf2, int sz)
{
	for (int i = 0; i < sz; i++)
	{
		char* temp = *(char*)buf1;
		*buf1= *buf2;
		*buf2= temp;
		buf1++;
		buf2++;
	}
}

void my_qsort(void* arr, int num, int sz, int (*cmp)(const void* buf1, const void* buf2))
{
	int i;
	for (i = 0; i < num - 1; i++)
	{
		for (int j = 0; j < num - 1 - i; j++)
		{
			if (cmp((char*)arr + j * sz, (char*)arr + (j + 1) * sz) > 0)
			{
				swap((char*)arr + j * sz, (char*)arr + (j + 1) * sz, sz);
			}
		}
	}
}

void text1()
{
	int arr[] = { 9,8,7,6,5,4,3,2,1,8 };
	print(arr, sizeof(arr) / sizeof(arr[0]));
	my_qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), num_sort);
	print(arr, sizeof(arr) / sizeof(arr[0]));
	return 0;
}

void text2()
{
	int i;
	stu arr[] = { {"zhangsan",20},{"lisi",18},{"quanqing",21} };
	for (i = 0; i < 3; i++)
	{
		printf("%d name:%s age:%d\n", i + 1, arr[i].name, arr[i].age);
	}
	my_qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), age_sort);
	for (i = 0; i < 3; i++)
	{
		printf("%d name:%s age:%d\n", i + 1, arr[i].name, arr[i].age);
	}
}
void text3()
{
	int i;
	stu arr[] = { {"zhangsan",20},{"lisi",18},{"quanqing",21} };
	for (i = 0; i < 3; i++)
	{
		printf("%d name:%s age:%d\n", i + 1, arr[i].name, arr[i].age);
	}
	my_qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), name_sort);
	for (i = 0; i < 3; i++)
	{
		printf("%d name:%s age:%d\n", i + 1, arr[i].name, arr[i].age);
	}
}

int main()
{
	text1();
	printf("\n");
	text2();
	printf("\n");
	text3();
	return 0;
}